//
//  ViewController.m
//  UIView的坐标转换
//
//  Created by 李昊林 on 16/7/26.
//  Copyright © 2016年 李昊林. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong)UIButton *buttonOfChild;
@property (nonatomic, strong)UIView *viewOfFather;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.viewOfFather = [[UIView alloc] initWithFrame:CGRectMake(40, 50, self.view.frame.size.width - 80, self.view.frame.size.height - 100)];
    self.viewOfFather.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.viewOfFather];
    
    
    self.buttonOfChild = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfChild.frame = CGRectMake(40, 50, 100, 50);
    self.buttonOfChild.backgroundColor = [UIColor redColor];
    [self.viewOfFather addSubview:self.buttonOfChild];
    [self.buttonOfChild addTarget:self action:@selector(clickedButtton:)forControlEvents:UIControlEventTouchUpInside];
}
- (void)clickedButtton:(UIButton *)button {
    
    //调用者是该rect的父视图 view参数是你要转换的视图
    //注意
    //1.supreview的写法
    //2.当view这个参数为nil的时候，默认转换为当前窗口，（即屏幕）
    //3.当view不为空的时候，调用者和View参数必须为同一UIWindow的对象
    CGRect newRect1 = [_buttonOfChild.superview convertRect:_buttonOfChild.frame toView:self.view];
    NSLog(@"%f,%f,%f,%f",newRect1.origin.x,newRect1.origin.y,newRect1.size.width,newRect1.size.height);
    _buttonOfChild.frame = newRect1;
    [self.view addSubview:_buttonOfChild];
    
    //fromView
    //这里调用者和View参数相反
    CGRect newRect2 = [self.view convertRect:_buttonOfChild.frame fromView:_viewOfFather];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end